/*******************************************************************\
* file: 	atalkxcmd.h												*
* version:	1.06§													*
*																	*
* Header file for the AppleTalk/Hypercard interface.   Defines some	*
* lowmem globals (PortBUse and SPCongfig) and other resource stuff.	*												*
* -----------------------------------------------------------------	*
* By:	Donald Koscheka												*
* Date:	21-Sept-87													*
* ©	Copyright 1987, Apple Computer, Inc.							*
*	All Rights Reserved												*
* -----------------------------------------------------------------	*
*						Modification History						*
* -----------------------------------------------------------------	*
*  Date	   	| By	|	 				Description					*
* -----------------------------------------------------------------	*
* 21-Sep-87	| DK	| file created									*
* 31-Sep-87 | DK	| modified Masterblock for preferred calls		*
*  1-Dec-87	| DK	| modified Client waiting queue to FIFO struct	*
*  2-Dec-87	| DK	| added 4K response structures to server and	*
*			|		| client transaction blocks.					*
*  7-Dec-87	| DK	| converted NBP to Parm block implemention		*
*  8-Dec-87 | DK	| "escalation" flag for server isInUse field	*
* 11-Dec-87	| DK	| converted nonrel objects in MasterBlock to 	*
*			| DK	| pointers from locked handles.	Client Response	*
*			|		| buffer converted to array from handle			*
* 10-Feb-88	| DK	| Added a flag that is set from ATPReceive to 	*
*			| 		| tell atpclose to hold off (in case it is 		*
*			|		| called as part of a callback) & which ATPclose*
*			| 		| sets to tell atpreceive to close down.		*
* -----------------------------------------------------------------	*
\*******************************************************************/

#include	<AppleTalk.h>

#define		PortBUse	0x291
#define		SPConfig	0x1FB
#define		ASYNC		1
#define		SYNC		0
#define		NODE_NAME	-16096		/* string resource with node name from Chooser	*/
#define		MAXNODES	100			/* maximum # of nodes  for this zone			*/
#define		NBPLSIZE	120			/* size of a local buffer for NBP				*/
#define		NN			30			/* number of names in the lookup table			*/
#define		CQSIZE		4			/* number of client transaction blocks			*/
#define		SQSIZE		8			/* number of server transaction blocks			*/
#define		ENTITYSIZE	110			/* size of an entity in the lookup buffer		*/
#define		ATPBSIZE	578			/* size of an atp record buffer					*/
#define		MAXBDS		8			/* maximum # of elements in a BDS Structure		*/
#define		AUTOSIZE	5			/* size of the autoresponse data				*/

#define		CLIENT		1			/* node to be connected as a client only		*/
#define		SERVER		2			/* node to be connected as a server only		*/

#define		CLOSE_OK	0			/* not a "CLOSE" critical routine				*/
#define		RECEIVING	1			/* currently executing ATPReceive				*/
#define		CLOSE_NOW	2			/* tells atpReceive to close down when done		*/

/*** Server isInUse escalation strategy 	***/
#define		IDLE			0
#define		WAIT_REQUEST	1
#define		WAIT_RESPONSE	2

#define		empty			0L		/* empty is an alias for  NIL in hypercard 		*/


#define		GlobalRspData	3728
#define		GlobalRcvData	3279


typedef	unsigned char	Byte;

	/***  			----- Client Side Waiting Queue Element	-----			 	  ***/
	/***  			Note the use of a singly linked-list - A FIFO doesn't		  ***/
	/***			need a back link (always add to the end, take away from		  ***/
	/***			the beginning).												  ***/

typedef	struct{									/* list element for client queue 	*/
		struct ClientQRec 	**next;				/* handle of the next element		*/
		char				**buf;				/* index to data buffer				*/
		long				size;				/* index to bds array				*/
		Handle				rsp;				/* handle to the response message	*/
		AddrBlock			addr;
		short				count;
		short				interval;
	}ClientQRec, *ClientQPtr, **ClientQH;
		
	
	/***  				----- Server Side Transaction Block	-----				  ***/
typedef	struct{
		short			isInUse;
		ATPParamBlock	pb;						/* PB for getrequest/send response	*/
		BDSType			rspBDS;					/* response buffer	data structure	*/
		char			buf[ATPBSIZE];			/* buffer for client request data	*/
		char			rspBuf[ATPBSIZE*MAXBDS];/* response data buffer				*/
	}ServerBlock, *ServerPtr;


	/***  				----- Client Side Transaction Block	-----				  ***/
typedef	struct{
		short			isInUse;
		ATPParamBlock	pb;						/* PB for PSendRequest				*/
		BDSType			bds;					/* response buffer data structure	*/
		char			buf[ATPBSIZE];			/* data sent in this request		*/
		char			rsp[ATPBSIZE*MAXBDS];	/* response to this request			*/
		Handle			rspM;					/* handle to the response message	*/
	}ClientBlock, *ClientPtr;
	
	
	/***  				----- A Hopefully Enlightening Note	-----				  ***/
	/***																		  ***/
	/***	You're probably wondering why we have two types of client structure	  ***/
	/***	(1) ClientQRec and (2) ClientBlock.  Well, the reason is that		  ***/
	/***	we can have only a finite number of requests "hopping" at a time 	  ***/
	/***	(because we can only have a finite number of sockets open at a time). ***/
	/***	The Client Blocks are the actual structures used in the transaction	  ***/
	/***	and are referred to as TRANSACTION BLOCKS throughout the code.		  ***/
	/***																		  ***/
	/***	Because we cannot have more than about 10 such transactions active	  ***/
	/*** 	at a time, we need to keep track of which requests are assigned		  ***/
	/***	a transaction block and which requests are waiting for a block		  ***/
	/***	(because we allow the user to issue more requests than we can have	  ***/
	/*** 	sockets open.  If a transaction block is available, the request is 	  ***/
	/*** 	said to be "active" (that is, queued up by AppleTalk),  If we have	  ***/
	/*** 	to pend the request, we keep it in our waiting queue.				  ***/


typedef	struct {								/* ATP protocol data				*/
		ServerPtr		Server;					/* array/server transaction blocks	*/
		ClientPtr		Client;					/* array/client transaction blocks	*/
		ClientQH		FirstRequest;			/* SendRequest wait "q"(client use)	*/
		ClientQH		LastRequest;			/* Last element in the waiting "q"	*/		
		short			checkPoint;				/* whehter in ATPReceive or ATPClose*/
		AddrBlock		ServerAddr;				/* address of the server socket		*/
		char			autoRsp[256];			/* the autorespond message			*/
}ATPBlock;

typedef	struct{									/* Name Binding Protocol Structures	*/
		short			Registered;				/* true if we are already registered*/
		short			EntCount;				/* number of entities visible		*/
		Handle			LookUpBuffer;			/* handle to the lookup buffer		*/
		NamesTableEntry	NTEntry;				/* entry into the names table		*/
		ZonesTable		;						/* table of visible zones			*/
		char			NBPLocal[NBPLSIZE];		/* used internally by NBP			*/
}NBPBlock;
		

ATPBlock	*ATPInit();
short		ATPKill();
short 		Request();
short		PollRequests();
short		Respond();

NBPBlock	*NBPInit();
short		NBPKill();
short		ConfirmName();
short 		ExtractName();
short		Lookup();
short		NodeRegister();
